package org.csu.jpetstore.bo;

import lombok.Data;
import org.csu.jpetstore.entity.Order;
import org.csu.jpetstore.entity.OrderStatus;
import org.csu.jpetstore.vo.LineItemVO;
import org.csu.jpetstore.vo.OrderFormVO;
import org.csu.jpetstore.vo.OrderVO;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author huangzhangyan
 */
@Data
public class OrderBO {
    private Integer orderId;
    private String userId;
    private java.sql.Date orderDate;
    private String shipProvince;
    private String shipCity;
    private String shipCounty;
    private String shipDetailAddress;
    private BigDecimal totalPrice;
    private String shipToFirstname;
    private String shipToLastname;
    /**
     * line_item表,order_status表中的
     */
    private List<LineItemBO> lineItemBOList;

    private OrderBO(Order order, List<LineItemBO> lineItemList) {
        // Order 部分
        this.orderId = order.getOrderId();
        this.userId = order.getUserId();
        this.orderDate = order.getOrderDate();
        this.shipProvince = order.getShipProvince();
        this.shipCity = order.getShipCity();
        this.shipCounty = order.getShipCounty();
        this.shipDetailAddress = order.getShipDetailAddress();
        this.totalPrice = order.getTotalPrice();
        this.shipToFirstname = order.getShipToFirstname();
        this.shipToLastname = order.getShipToLastname();

        this.lineItemBOList = lineItemList;
    }

    private OrderBO(OrderFormVO orderFormVO, List<LineItemVO> lineItemVOList) {
        //orderId 在之后读取数据库生成
        //OrderFormVO
        this.userId = orderFormVO.getUserId();
        this.orderDate = orderFormVO.getOrderDate();
        this.shipProvince = orderFormVO.getShipProvince();
        this.shipCity = orderFormVO.getShipCity();
        this.shipCounty = orderFormVO.getShipCounty();
        this.shipDetailAddress = orderFormVO.getShipDetailAddress();
        //计算总价
        double total = 0;
        for (LineItemVO lineItemVO : lineItemVOList) {
            total += lineItemVO.getQuantity() * lineItemVO.getListPrice().doubleValue();
        }
        orderFormVO.setTotalPrice(new BigDecimal(total));
        this.totalPrice = orderFormVO.getTotalPrice();
        this.shipToFirstname = orderFormVO.getShipToFirstname();
        this.shipToLastname = orderFormVO.getShipToLastname();

        //List<LineItemVO>
        List<LineItemBO> lineItemBOList = new ArrayList<>();
        for (LineItemVO lineItemVO : lineItemVOList) {
            LineItemBO lineItemBO = LineItemBO.toLineItemBO(lineItemVO);
            lineItemBOList.add(lineItemBO);
        }

        this.lineItemBOList = lineItemBOList;
    }

    public static OrderBO toOrderBO(Order order, List<LineItemBO> lineItemBOList) {
        if (order == null || lineItemBOList == null)
            return null;
        return new OrderBO(order, lineItemBOList);
    }

    public static OrderBO toOrderBO(OrderFormVO orderFormVO, List<LineItemVO> lineItemVOList) {
        if (orderFormVO == null || lineItemVOList == null)
            return null;
        return new OrderBO(orderFormVO, lineItemVOList);
    }

    /**
     * 弃用的
     */
    @Deprecated
    public static List<OrderStatus> toOrderStatusList(OrderBO orderBO) {
        List<OrderStatus> orderStatusList = new ArrayList<>();

        for (LineItemBO lineItemBO : orderBO.getLineItemBOList()) {
            OrderStatus orderStatus = new OrderStatus();
            orderStatus.setOrderId(orderBO.getOrderId());
            orderStatus.setLineNumber(lineItemBO.getLineNumber());
            orderStatus.setTimeStamp(orderBO.getOrderDate());
            orderStatus.setStatus(lineItemBO.getStatus());
            orderStatusList.add(orderStatus);
        }
        return orderStatusList;
    }

    public static Order toOrderEntity(OrderBO orderBO) {
        Order orderEntity = new Order();
        orderEntity.setOrderId(orderBO.getOrderId());
        orderEntity.setUserId(orderBO.getUserId());
        orderEntity.setOrderDate(orderBO.getOrderDate());
        orderEntity.setShipProvince(orderBO.getShipProvince());
        orderEntity.setShipCity(orderBO.getShipCity());
        orderEntity.setShipCounty(orderBO.getShipCounty());
        orderEntity.setShipDetailAddress(orderBO.getShipDetailAddress());
        orderEntity.setTotalPrice(orderBO.getTotalPrice());
        orderEntity.setShipToFirstname(orderBO.getShipToFirstname());
        orderEntity.setShipToLastname(orderBO.getShipToLastname());
        return orderEntity;
    }

    public static OrderFormVO toOrderFormVO(OrderBO orderBO) {
        OrderFormVO orderFormVO = new OrderFormVO();
        orderFormVO.setOrderId(orderBO.getOrderId());
        orderFormVO.setUserId(orderBO.getUserId());
        orderFormVO.setOrderDate(orderBO.getOrderDate());
        orderFormVO.setShipProvince(orderBO.getShipProvince());
        orderFormVO.setShipCity(orderBO.getShipCity());
        orderFormVO.setShipCounty(orderBO.getShipCounty());
        orderFormVO.setShipDetailAddress(orderBO.getShipDetailAddress());
        orderFormVO.setTotalPrice(orderBO.getTotalPrice());
        orderFormVO.setShipToFirstname(orderBO.getShipToFirstname());
        orderFormVO.setShipToLastname(orderBO.getShipToLastname());
        return orderFormVO;
    }

    public static OrderVO toOrderVO(OrderBO orderBO) {
        List<LineItemVO> lineItemVOList = new ArrayList<>();
        boolean isPay = true;
        for (LineItemBO lineItemBO : orderBO.getLineItemBOList()) {
            LineItemVO lineItemVO = LineItemBO.toLineItemVO(lineItemBO);
            lineItemVOList.add(lineItemVO);
            if ("待付款".equals(lineItemBO.getStatus()))
                isPay = false;
        }
        return new OrderVO(OrderBO.toOrderFormVO(orderBO), lineItemVOList, isPay);
    }

}
